<?php
namespace common\models;

use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use common\component\tool\Tool;


class AlbumCategory extends \yii\db\ActiveRecord
{

    public static function getDb()
    {
        return Yii::$app->get("common_db");
    }
    
	public static function tableName()
    {
        return '{{%album_category}}';
    }

	public function rules()
    {
        return [
            ['cat_name', 'required', 'message' => 'cat_name不能为空', 'on' => 'add'],
            ['cat_name', 'string', 'message' => 'cat_name必须是字符串',
                         'max' => 64, 'tooLong' => 'cat_name最长64位'],
            ['cat_name', 'validateCatnameRepeat', 'on' => 'add'],
            ['cover_url', 'default', 'value' => ''],
            ['cover_url', 'string', 'message' => 'cover_url必须是字符串',
                          'max' => 256, 'tooLong' => 'cover_url最长256位'],
            ['state', 'default', 'value' => 2],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2, 3), 'message' => 'state值必须在1,2,3中选择'],
            ['password', 'default', 'value' => ''],
            ['password', 'string', 'message' => 'password必须是字符串',
                         'max' => 32, 'tooLong' => 'password最长32位'],
            ['album_number', 'integer', 'message' => 'album_number必须为数字，或数字字符串', 
                             'min'=>0, 'tooSmall' => 'album_number最小存储0', 
                             'max'=>4294967295, 'tooBig' => 'album_number最大存储4294967295'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>4294967295, 'tooBig' => 'sort最大存储4294967295'],            
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
            ['app_id', 'default', 'value' => 0],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>4294967295, 'tooBig' => 'app_id最大存储4294967295'],
            ['com_id', 'default', 'value' => 0],
            ['com_id', 'integer', 'message' => 'com_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'com_id最小存储0', 
                        'max'=>4294967295, 'tooBig' => 'com_id最大存储4294967295'],
        ];
    }

    /*rules 验证相册名是否存在*/
    public function validateCatnameRepeat()
    {
        if(!isset($this->app_id)){
            $this->app_id = 0;
        }
        if(!$this->hasErrors()){
            $data = self::find()
                    ->where('cat_name = :name and app_id = :app_id and com_id = :com_id', [':name' => $this->cat_name, ':app_id' => $this->app_id, ':com_id' => $this->com_id])
                    ->one();
            if(!is_null($data)){
                $this->addError('repeatName', '该名称已经存在');
            }
        }
    }

    /*添加相册列表*/
    public function addCategory($data, $com_id = 0, $app_id = 0)
    {
        $data['AlbumCategory']['com_id'] = $com_id;
        $data['AlbumCategory']['app_id'] = $app_id;
        $data['AlbumCategory']['add_time'] = date('Y-m-d H:i:s', time());
        $this->scenario = 'add';
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'cat_id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }


}
?>